import os
#import sys
#from src.codeql_tools import config

# dbName=sys.argv[1]
# dbDirName=sys.argv[2]
# scriptName=sys.argv[3]

#output_dir=config.query_dir
temp_dir='temp'

def get_query_script(script_name,language='python'):
    return f'CodeQuery/{language}/{script_name}.ql'

def query_codeql_db(codeql_bin,codeql_db_path,script_name,output_dir):
    execute_cmd='{} query run --database={} --output={} {}'
    decode_cmd='{} bqrs decode --format={} --output={} {}'
    
    temp_path='temp/{}.bqrs'.format(script_name)
    
    script_path=get_query_script(script_name)
    query_result_path='{}/{}.csv'.format(output_dir,script_name)
    
    
    execute_cmd=execute_cmd.format(codeql_bin,codeql_db_path,script_path)
    decode_cmd=decode_cmd.format(codeql_bin,"csv",query_result_path,temp_path)

    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)

    os.system(execute_cmd)
    os.system(decode_cmd)
    
def create_codeql_db(codeql_bin,codeql_db_dir,codeql_db_name,source_dir,language='python'):    
    if not os.path.exists(codeql_db_dir):
        os.makedirs(codeql_db_dir)
    
    cmd=f'{codeql_bin} database create {os.path.join(codeql_db_dir,codeql_db_name)} --language={language} --source-root={source_dir} --overwrite'
    
    os.system(cmd)
    
#dbName=r"C:\Users\gxw19\AppData\Roaming\Code\User\workspaceStorage\f6534c56f76be6a03cf0c7aad8ebb9a2\GitHub.vscode-codeql\naman873_Python_python-srcVersion_7054c838bb7a5484961a2672d80afd9f91cc791e-dist_codeql-bundle-linux64-20220401-2077884817\naman873_Python_7054c83"
# formatName='csv'
# outputPath='temp/{}.bqrs'.format(scriptName)
# qlPath='CodeQuery/python/{}.ql'.format(scriptName)


# resultPath='{}/{}.csv'.format(dbDirName,scriptName)

# if not os.path.exists(dbDirName):
#     os.makedirs(dbDirName)

# execute_cmd='{} query run --database={} --output={} {}'

# decode_cmd='{} bqrs decode --format={} --output={} {}'

# execute_cmd=execute_cmd.format(config.codeql_path,dbName,outputPath,qlPath)
# decode_cmd=decode_cmd.format(config.codeql_path,formatName,resultPath,outputPath)

# print(execute_cmd)
# print(decode_cmd)

# os.system(execute_cmd)
# os.system(decode_cmd)